import 'package:dk_flutter_base/dk_core.dart';
import 'package:flutter/services.dart';

class NotStartWith0InputFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    return objNotEmpty(newValue) &&
            stringNotEmpty(newValue.text) &&
            !newValue.text.startsWith("0")
        ? newValue
        : TextEditingValue();
  }
}

class LimitNumScopeInputFormatter extends TextInputFormatter {
  num start;
  num end;

  LimitNumScopeInputFormatter(this.start, this.end);

  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    TextEditingValue value;
    if (objEmpty(newValue) || stringBlank(newValue.text)) {
      value = TextEditingValue();
      return value;
    }
    try {
      num cur = num.parse(newValue.text);
      if (cur >= start && cur <= end) {
        value = newValue;
      }
    } catch (e) {
      print(e.toString());
    }
    return value ?? oldValue;
  }
}
